package com.mediacloud.app.newsmodule.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.util.TypedValue
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.GsonUtils
import com.flyco.tablayout.listener.OnTabSelectListener
import com.google.android.material.appbar.AppBarLayout
import com.luck.picture.lib.tools.DoubleUtils
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.MagazineEventBusData
import com.mediacloud.app.model.activity.BaseBackActivity
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.MagazineProgramFragment
import com.mediacloud.app.newsmodule.fragment.MagazineReviewFragment
import com.mediacloud.app.newsmodule.model.MagazineProgramBean
import com.mediacloud.appcloud.project.gxapp.model.utils.Tools
import com.yft.PagerAdapter
import kotlinx.android.synthetic.main.activity_magazine_program_list.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * 精选榜单列表
 */
class MagazineProgramActivity : BaseBackActivity() {




    val fatherId by lazy {
        intent.getStringExtra("magazineID")
    }

    override fun getLayoutResID(): Int {
        return R.layout.activity_magazine_program_list
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }

        top_back.setOnClickListener {
            finish()
        }
        introduce.setOnClickListener {
            if(DoubleUtils.isFastDoubleClick()){
                return@setOnClickListener
            }
            val intent = Intent(this@MagazineProgramActivity, MagazineIntroduceActivity::class.java)
            if(data!=null){
                intent.putExtra("data",data)
            }
            startActivity(intent)
        }

        val fragmentList: MutableList<Fragment> = ArrayList()
        fragmentList.add(MagazineProgramFragment.getInstance(fatherId))
        fragmentList.add(MagazineReviewFragment.getInstance(fatherId))
        val titleList: MutableList<String> = ArrayList()
        titleList.add("目录")
        titleList.add("往期回顾")
        viewPager.adapter = PagerAdapter(fragmentList, titleList, supportFragmentManager)
        mTabLayout.setViewPager(viewPager)
        viewPager.offscreenPageLimit = titleList.size
        mTabLayout.getTitleView(0).setTextSize(TypedValue.COMPLEX_UNIT_SP,17f)
        mTabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                titleList.forEachIndexed{ index, _ ->
                    if (index == position)
                        mTabLayout.getTitleView(position).setTextSize(TypedValue.COMPLEX_UNIT_SP,17f)
                    else
                        mTabLayout.getTitleView(index).setTextSize(TypedValue.COMPLEX_UNIT_SP,15f)

                }
                Tools.hideSoftInput(this@MagazineProgramActivity)
            }
            override fun onTabReselect(position: Int) {

            }

        })

        val yOff = intArrayOf(0)
        mCollapsingToolbarLayout?.post {
            appbar?.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
                if (yOff[0] <= 0) {
                    if (mCollapsingToolbarLayout != null && toolbar != null) {
                        yOff[0] =
                            mCollapsingToolbarLayout!!.measuredHeight - toolbar!!.height
                    }
                }
                if (Math.abs(verticalOffset) >= yOff[0]) {
                    toolbar.background = ContextCompat.getDrawable(this@MagazineProgramActivity,R.color.white)
                } else {
//                    val rate = Math.abs(verticalOffset * 1f / yOff[0])
                    toolbar.background = ContextCompat.getDrawable(this@MagazineProgramActivity,R.color.transparent)
                }
            })
        }
    }




    override fun getFitSystemWindow(): Boolean {
        return false
    }


    override fun getStatusBarColor(): Int {
        return Color.TRANSPARENT
    }

    var data : MagazineProgramBean.IntroFieldDtoData ?= null

    @Subscribe(threadMode = ThreadMode.MAIN)
     fun action(action : MagazineEventBusData) {
        if(!TextUtils.isEmpty(action.json.optString("prop1"))){
            summary.setContent(action.json.optString("prop1"))
            summary.visibility = View.VISIBLE
        }

        if(!TextUtils.isEmpty(action.json.optString("magazineName"))){
            tv_title.text = action.json.optString("magazineName")
            tv_title.visibility = View.VISIBLE
        }
        if(!TextUtils.isEmpty(action.json.optString("imagePath"))){
            ivLogo.loadX(action.json.optString("imagePath"), 1, 1, true, null)
        }
        if(!TextUtils.isEmpty(action.json.optString("name"))){
            tv_content.text = action.json.optString("name")
            tv_content.visibility = View.VISIBLE
        }
        introduce.isVisible = !action.json.optString("prop2").equals("0")
        val introFieldDto = action.json.optJSONObject("introFieldDto")
        introFieldDto?.let {
            data = GsonUtils.fromJson(it.toString(), MagazineProgramBean.IntroFieldDtoData::class.java)
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
//        newsListItemStyleAdaptor?.destroy()
    }
}